// pages/service/service.js
let url_js = require("../../utils/url.js");
let user_js = require("../../utils/user.js");
let common_js = require("../../utils/common.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let api = require("../../utils/apiRequest.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    shopInfo: null,
    sysInfo: null,
    contentStatus: -1, //1有数据
    remark: '',
    serviceList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);

    //1.禁止转发
    wx.hideShareMenu();
    //2.初始化数据
    this.initData(options);
    //3.请求数据
    this.requestToGetCustomerList();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },


  //初始化数据
  initData: function () {
    let tempUserInfo = user_js.userInfo();
    this.setData({
      userInfo: tempUserInfo
    });
  },

  //请求数据
  requestToGetCustomerList: function () {
    //Promise.all
    // shopInfo
    let data = {
      id: this.data.userInfo.ztd.id
    }
    // 客服
    let customerData = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      shopId: this.data.userInfo.ztd.id
    }
    Promise.all([api.wxRequest(url_js.shopInfoUrl, data), api.wxRequest(url_js.customerListUrl, customerData)]).then(results => {
      this.handleShopInfo(results[0]);
      this.handleCustomerList(results[1]);
    }).catch(err => {
      common_js.log(err);
    });
  },

  // 处理返回的店铺信息
  handleShopInfo(res) {
    this.setData({
      shopInfo: res.result
    })
  },

  //处理返回的客服结果 
  handleCustomerList: function (res) {
    let tmpList = res.result;
    if (tmpList.length > 0) {
      this.setData({
        contentStatus: 1,
        remark: res.remark,
        serviceList: tmpList
      })
    }
  },

  //复制
  onCopy: function (e) {
    if (!this.data.shopInfo.wechatNo) {
      common_js.showToast('微信号不存在');
      return;
    }

    //设置剪切板
    wx.setClipboardData({
      data: this.data.shopInfo.wechatNo,
      success: res => {
        common_js.showToast('微信号已复制');
      }
    });
  }
})